package com.genius.dingding.util;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiDepartmentCreateRequest;
import com.dingtalk.api.request.OapiDepartmentDeleteRequest;
import com.dingtalk.api.request.OapiDepartmentGetRequest;
import com.dingtalk.api.request.OapiDepartmentListIdsRequest;
import com.dingtalk.api.request.OapiDepartmentListRequest;
import com.dingtalk.api.response.OapiDepartmentCreateResponse;
import com.dingtalk.api.response.OapiDepartmentDeleteResponse;
import com.dingtalk.api.response.OapiDepartmentGetResponse;
import com.dingtalk.api.response.OapiDepartmentListIdsResponse;
import com.dingtalk.api.response.OapiDepartmentListResponse;
import com.dingtalk.api.response.OapiDepartmentListResponse.Department;
import com.taobao.api.ApiException;

import java.util.ArrayList;
import java.util.List;

/**
 * 部门相关API
 *
 * https://open-doc.dingtalk.com/docs/doc.htm?treeId=371&articleId=106817&docType=1
 */
public class DepartmentHelper {

    /**
     *  创建部门
     */
    public static OapiDepartmentCreateResponse createDepartment(String accessToken, String name,
                                          String parentId, String order, boolean createDeptGroup) throws Exception {
    	DingTalkClient client = new DefaultDingTalkClient(GeniusConfig.OAPI_HOST+"/department/create");
        OapiDepartmentCreateRequest request = new OapiDepartmentCreateRequest();
        request.setParentid(parentId);
        request.setCreateDeptGroup(createDeptGroup);
        request.setOrder(order);
        request.setName(name);
        OapiDepartmentCreateResponse response = client.execute(request, accessToken);
        return response;
    }

    /**
     * 获取部门列表
     */
    public static List<Department> listDepartments(String accessToken, String parentDeptId){
        
    	DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/department/list");
        OapiDepartmentListRequest request = new OapiDepartmentListRequest();
        request.setId(parentDeptId);
        request.setHttpMethod("GET");
        OapiDepartmentListResponse response;
        List<Department> deptList = null;
		try {
			response = client.execute(request, accessToken);
			if(response.getErrcode() == null && !response.getErrcode().equals("0")){
				deptList = new ArrayList<Department>();
			}else{
				deptList = response.getDepartment();
			}
		} catch (ApiException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return deptList;
    }
    /**
     * 获取部门列表带是否
     */
    public static List<Department> listDepartments(String accessToken, String parentDeptId,Boolean fetchChild){
        
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/department/list");
        OapiDepartmentListRequest request = new OapiDepartmentListRequest();
        request.setId(parentDeptId);
        request.setHttpMethod("GET");
        request.setFetchChild(fetchChild);
        OapiDepartmentListResponse response;
        List<Department> deptList = null;
		try {
			response = client.execute(request, accessToken);
			if(response.getErrcode() == null && !response.getErrcode().equals("0")){
				deptList = new ArrayList<Department>();
			}else{
				deptList = response.getDepartment();
			}
		} catch (ApiException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return deptList;
    }
    /**
     * 获取子部门列表
     */
    public static List<Long> listDepartmentids(String accessToken, String parentDeptId){
        
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/department/list_ids");
        OapiDepartmentListIdsRequest request = new OapiDepartmentListIdsRequest();
        request.setId(parentDeptId);
        request.setHttpMethod("GET");
        OapiDepartmentListIdsResponse response;
        List<Long> deptList = null;
		try {
			response = client.execute(request, accessToken);
			if(response.getErrcode() == null && !response.getErrcode().equals("0")){
				deptList = new ArrayList<Long>();
			}else{
				deptList = response.getSubDeptIdList();
			}
		} catch (ApiException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return deptList;
    }
    /**
     * 获取部门详情
     */
    public static OapiDepartmentGetResponse getDepartment(String accessToken, String deptId){
    	DingTalkClient client = new DefaultDingTalkClient(GeniusConfig.OAPI_HOST+"/department/get");
    	OapiDepartmentGetRequest request = new OapiDepartmentGetRequest();
    	request.setId(deptId);
    	request.setHttpMethod("GET");
    	OapiDepartmentGetResponse response = null;
    	try {
			response = client.execute(request, accessToken);
		} catch (ApiException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return response;
    }
    /**
     * 删除部门
     */
    public static void deleteDepartment(String accessToken, String id) throws Exception {
    	DingTalkClient client = new DefaultDingTalkClient(GeniusConfig.OAPI_HOST+"/department/delete");
        OapiDepartmentDeleteRequest request = new OapiDepartmentDeleteRequest();
        request.setId(id);
        request.setHttpMethod("GET");
        OapiDepartmentDeleteResponse response = client.execute(request, accessToken);
    }

}
